/*
 * @Author: vera_min vera_min@163.com
 * @Date: 2025-07-20 21:21:52
 * @LastEditors: vera_min vera_min@163.com
 * @LastEditTime: 2025-08-26 19:23:59
 * @FilePath: /mail_project_destop/src/context/sse.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ElNotification } from "element-plus";
import Cookies from "/@/utils/Cookies";
import SSEFetcher from "/@/utils/sseFetcher";
import { refreshMenuList } from '/@/context/globalEvent';


const connectSSE = (callback: Function) => {
  const userId = Cookies.get('userId');
  const url = `/email/api/see/subscribe/${userId}`;
  const eventSource = new SSEFetcher(import.meta.env.VITE_API_URL + url, (res) => {
      const json = JSON.parse(res);
      ElNotification({
        title: json.msg,
        message: `${json.account}收到来自${json.fromEmail}的邮件`,
      });
      
      callback(json);

      refreshMenuList(json);
  });
};

export default connectSSE;